{
 "cells": [
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-03T10:51:52.154269Z",
     "start_time": "2025-09-03T10:51:50.892081Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#symotion-s) 数据特征通常用 32位浮点类型表示，数据的标签通常用 64位的整形表示\n",
    "# torch.Tensor 含有 hymenoptera\\dtype\\shape\\device\\requires_grad\\grad\\grad_fn\\is_leaf这几个属性\n",
    "import torch\n",
    "import numpy as np"
   ],
   "id": "d5bc759af217b844",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.452055Z",
     "start_time": "2025-09-03T07:43:18.238661Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 创建张量\n",
    "# 直接创建\n",
    "arr = np.ones((3, 3))\n",
    "arr.dtype"
   ],
   "id": "11eac8cbef30570b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.468122Z",
     "start_time": "2025-09-03T07:43:19.461458Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在 cpu 上保存数据\n",
    "t = torch.tensor(arr)\n",
    "t.dtype"
   ],
   "id": "c10ca65f35663b93",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.708288Z",
     "start_time": "2025-09-03T07:43:19.487086Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在 GPU 上保存\n",
    "t1 = torch.tensor(arr, device=torch.device('mps'), dtype=torch.float32)\n",
    "t1"
   ],
   "id": "34a7d9099aab69d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.]], device='mps:0')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.714450Z",
     "start_time": "2025-09-03T07:43:19.712325Z"
    }
   },
   "cell_type": "code",
   "source": "# 从 numpy 中创建 tensor",
   "id": "3c4f00b5246fc92b",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.746510Z",
     "start_time": "2025-09-03T07:43:19.742615Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "t2 = torch.from_numpy(arr1)\n",
    "t2.dtype"
   ],
   "id": "3afcc77bbd8408cd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:19.766894Z",
     "start_time": "2025-09-03T07:43:19.762919Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 这种情况下创建的 tensor 和 np array共用一个内存，因此如果修改 tensor 数据，则原始的 np array也会跟着发生变化\n",
    "t2[0, 0] = 100\n",
    "t2\n"
   ],
   "id": "d15643df72dfe2fc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[100,   2,   3],\n",
       "        [  4,   5,   6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T07:43:28.642676Z",
     "start_time": "2025-09-03T07:43:28.635931Z"
    }
   },
   "cell_type": "code",
   "source": "arr1",
   "id": "3e8b3eb8752ffc6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[100,   2,   3],\n",
       "       [  4,   5,   6]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T10:51:54.277862Z",
     "start_time": "2025-09-03T10:51:54.261121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 依据数值创建张量\n",
    "out_t = torch.tensor([1])\n",
    "t = torch.zeros((3, 3), out=out_t)\n",
    "print(out_t)\n",
    "print(t)\n",
    "print(id(out_t), id(t), id(out_t) == id(t))"
   ],
   "id": "1e66cd89368fe82e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n",
      "tensor([[0, 0, 0],\n",
      "        [0, 0, 0],\n",
      "        [0, 0, 0]])\n",
      "4924310288 4924310288 True\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T10:52:47.867720Z",
     "start_time": "2025-09-03T10:52:47.852353Z"
    }
   },
   "cell_type": "code",
   "source": [
    "input = torch.tensor([[1, 2, 3], [4, 5, 6]])\n",
    "output = torch.zeros_like(input, device=torch.device('mps'))\n",
    "output"
   ],
   "id": "dd5e1d60920c519b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 0, 0],\n",
       "        [0, 0, 0]], device='mps:0')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# torch.ones torch.ones_like 生成全 1 的向量\n",
    "# torch.full torch.full_like 生成指定 value 的向量\n",
    "# 这两种用法同 torch.zeros一样"
   ],
   "id": "c2d043bc9a76bb51"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T10:58:14.725086Z",
     "start_time": "2025-09-03T10:58:14.716299Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# torch.arange()生成等差序列\n",
    "odd_arr = torch.arange(2, 10, 2)\n",
    "odd_arr"
   ],
   "id": "a1b01611b32d1a09",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2, 4, 6, 8])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-03T14:58:26.708231Z",
     "start_time": "2025-09-03T14:58:26.702099Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# torch.linspace()生成指定区间范围内指定长度的序列，步长计算公式：(end-start) / (length-1)\n",
    "even_arr = torch.linspace(1, 11\n",
    "                          , 6)\n",
    "even_arr"
   ],
   "id": "6645aa847f359d65",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.,  3.,  5.,  7.,  9., 11.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
